package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmaelDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.MarshalException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmaelDishMapper setmaelDishMapper;

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page page = (Page) dishMapper.page(dishPageQueryDTO);
        //先获取总记录数 在获取数据集合转换成vo对象
        long total = page.getTotal();
        //获取总页数
        List<Dish> dishList = page.getResult();
        //遍历集合将数据封装到vo对象中
        ArrayList<DishVO> dishVOSList = new ArrayList<>();
        for (Dish dish : dishList) {
            //创建vo对象
            DishVO dishVO = new DishVO();
            //把数据复制到vo对象中
            BeanUtils.copyProperties(dish, dishVO);
            //查询当前菜品的分类名称存到vo对象中
            String categoryName = categoryMapper.selectByName(dish.getCategoryId());
            dishVO.setCategoryName(categoryName);
            dishVOSList.add(dishVO);
        }
        PageResult pageResult = new PageResult();
        pageResult.setRecords(dishVOSList);
        pageResult.setTotal(total);
        return pageResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        //口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.insert(flavors);
        }

    }

    @Override
    public List<DishVO> list(Long categoryId) {
        List<DishVO> list = dishMapper.list(categoryId);
        //查询一下菜品的口味儿
        for (DishVO dishVO : list) {
            List<DishFlavor> dishFlavorByid = dishFlavorMapper.getDishFlavorByid(dishVO.getId());
            dishVO.setFlavors(dishFlavorByid);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> ids) {
        //起售中的不能删除
        Integer statusCont = dishMapper.getstatusCont(ids);
        if (statusCont > StatusConstant.DISABLE) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //关联了套餐的菜品不能删除
        Integer setmealCont = setmaelDishMapper.getsetmealCont(ids);
        if (setmealCont >  StatusConstant.DISABLE) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
        dishMapper.delete(ids);
        //删除菜品关联的口味儿
        dishFlavorMapper.delete(ids);
    }

    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        //删除菜品关联的口味儿
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0){
            //批量删除所有菜的口味 在批量添加就可以了
            dishFlavorMapper.delete(Arrays.asList(dishDTO.getId()));
            //批量添加口味儿
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.insert(flavors);
        }
    }

    @Override
    public  DishVO getByid(Long id) {
        DishVO dishVO = new DishVO();
        //查询菜品信息
        Dish  dish = dishMapper.getDishByid(id);
        //查询口味信息
       List<DishFlavor> dishFlavor =  dishFlavorMapper.getDishFlavorByid(id);
       //把数据封装到vo对象中
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    @Override
    public void status(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
    }
}
